﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using JupiterLibrary;
using KinnectInput.KinectIcons;
using KinnectInput.KinectVideo;
using Microsoft.Research.Kinect.Nui;

namespace KinnectInput
{
    public enum KinectMenuState
    {
        Colapsed,
        Iniciated,
        Active

    }
    public class KinectMenuWindow
    {
        GraphicsDevice graphicsDevice;

        public Rectangle Background;

        public Rectangle AnimatedRect;
        Texture2D BackroundTex;
        public Sprite sprite;
        public KinectMenuState MenuState { get; set; }
        public int count { get; private set; }
        public int nColumns { get; set; }
        private int interLine;
        Texture2D[] Icons;
        public KinectButton[] IconsSprites { get; private set; }
        public bool[] wasUnSelected;
        KinectFlatInteraction flatHands;
        float width;
        float height;
        public KinectVideoPlayer player;
        public Animation sending;
        public bool IsCloseToTalayot = false;

        public KinectMenuWindow(KinectFlatInteraction flatHands)
        {
            #region MenuBar
            this.flatHands = flatHands;
            nColumns = 1;
            this.graphicsDevice = Jupiter.Video.GraphicsDevice;
            MenuState = KinectMenuState.Colapsed;
            sprite = new Sprite();
            Icons = new Texture2D[] { 
                Jupiter.Content.Load<Texture2D>("Icons/Plane"), 
                Jupiter.Content.Load<Texture2D>("Icons/Info"), 
                Jupiter.Content.Load<Texture2D>("Icons/Hotel"), 
                Jupiter.Content.Load<Texture2D>("Icons/Car"),
                Jupiter.Content.Load<Texture2D>("Icons/WindowsPhone"),
                Jupiter.Content.Load<Texture2D>("Icons/Computer")
                
            };
            wasUnSelected = new bool[Icons.Length];
            IconsSprites = new KinectButton[Icons.Length];

            int columnas = Icons.Length / 2;
            int size = graphicsDevice.Viewport.Width / (columnas + 1);
            this.Background = new Rectangle(0, (graphicsDevice.Viewport.Height / 2) - size, size * 2, graphicsDevice.Viewport.Width);
            AnimatedRect = Background;

            fillIcons();
            #endregion MenuBar

            Microsoft.Xna.Framework.Media.Video myMovie = Jupiter.Content.Load<Microsoft.Xna.Framework.Media.Video>("Video/Naveta");
            Microsoft.Xna.Framework.Media.Video sendingMovie = Jupiter.Content.Load<Microsoft.Xna.Framework.Media.Video>("Video/Enviar");
            float aspectRatio = (float)myMovie.Height / (float)myMovie.Width;
            int videoWidth = (graphicsDevice.Viewport.Width / 3) * 2;
            int xVideo=graphicsDevice.Viewport.Width/3;
            int yVideo=graphicsDevice.Viewport.Height/10;
            int width=graphicsDevice.Viewport.Width-xVideo;
            Rectangle rVideo=new Rectangle(xVideo/2,yVideo/2,width,(int)(width*aspectRatio));
            player = new KinectVideoPlayer(myMovie, rVideo);
            sending = new Animation(sendingMovie, rVideo);

        }
        private void fillIcons()
        {
            int width = graphicsDevice.Viewport.Height / 3;
            int margin = width*((Icons.Length / 2) +1);
            margin = graphicsDevice.Viewport.Width - margin;
            margin /=2;
            int vinyeta = 20;
            for (int i=0;i<Icons.Length;i++)
            {
                if (i < Icons.Length / 2)
                {
                    //int x = (margin * (i+1))-(width/2) + i * width;
                    int x = margin +(vinyeta* i) + i * width;
                    int y = (graphicsDevice.Viewport.Height/2) - (vinyeta / 2) - width;
                    IconsSprites[i] = new KinectButton(Icons[i], new Vector2(x, y));
                    IconsSprites[i].ButtonArea = new Rectangle(x, y, width, width);

                    
                }
                else
                {
                    int offset = i - Icons.Length / 2;
                    //offset +=(margin * (offset+1))-(width/2)+offset * width;
                    offset = margin + (vinyeta * offset ) + offset * width;
                    int y = (graphicsDevice.Viewport.Height / 2) + (vinyeta / 2) ;
                    IconsSprites[i] = new KinectButton(Icons[i], new Vector2(offset, y));
                    IconsSprites[i].ButtonArea = new Rectangle(offset, y, width, width);
                }
            }
        }
        public void UpdateProximityVideoPlayerButtons(Point p)
        {
            if (player.VideoPlayerState == KinectVideoPlayerState.Visible)
            {
                int resetX=graphicsDevice.Viewport.Bounds.Center.X;
                int resetY=graphicsDevice.Viewport.Bounds.Center.Y;
                VideoPlayerButton[] vpBtns = { player.ControlBar.PauseBtn, player.ControlBar.StopBtn, player.ControlBar.PlayBtn };
                foreach (VideoPlayerButton vpBtn in vpBtns)
                {
                    if (vpBtn.ButtonRect.Contains(p))
                    {
                        if (player.myPlayer.State == MediaState.Playing)
                        {
                            if (vpBtn == player.ControlBar.PauseBtn)
                                player.myPlayer.Pause();
                            else if(vpBtn == player.ControlBar.StopBtn)
                                player.myPlayer.Stop();
                        }
                        else
                        {
                            player.myPlayer.Play(player.movie);
                        }

                        //Mouse.SetPosition(resetX, resetY);
                    }
                }
                
            }
        }
        public void Update()
        {
            bool unselectFakeButtons = false;
            if (this.MenuState == KinectMenuState.Colapsed)
            {
                unselectFakeButtons = false;
                foreach(KinectButton kb in IconsSprites)
                {
                    kb.ButtonState = KinectButtonState.InActive;
                    kb.LastButtonState = KinectButtonState.InActive;
                }
                if (player.myPlayer.State == MediaState.Playing)
                {
                    player.myPlayer.Stop();
                }
                if (player.VideoPlayerState == KinectVideoPlayerState.Visible)
                {
                    player.VideoPlayerState = KinectVideoPlayerState.Colasped;
                }
                player.Update();
            }
            if (player.VideoPlayerState == KinectVideoPlayerState.Visible && player.myPlayer.State != MediaState.Playing)
            {
                foreach(KinectButton kb in this.IconsSprites)
                {
                    kb.ButtonState = KinectButtonState.Obsucred;
                }
                player.VideoPlayerState = KinectVideoPlayerState.Colasped;
            }
            else 
            {
                //Menu activo, videos no corriendo 
                if (MenuState == KinectMenuState.Active && player.VideoPlayerState!=KinectVideoPlayerState.Visible && this.sending.myPlayer.State!=MediaState.Playing)
                {
                    Point mousePos = new Point(Mouse.GetState().X, Mouse.GetState().Y);
                    for (int i = 0; i < IconsSprites.Length; i++)
                    {
                        if (IconsSprites[i].ButtonArea.Contains(mousePos))
                        {
                            IconsSprites[i].ButtonState = KinectButtonState.Selected;
                            //Mouse.SetPosition(graphicsDevice.Viewport.Bounds.Center.X, graphicsDevice.Viewport.Bounds.Center.Y);
                        }
                    }

                    if (IconsSprites[1].ButtonState == KinectButtonState.Selected && this.MenuState == KinectMenuState.Active)
                    {
                        player.VideoPlayerState = KinectVideoPlayerState.Visible;
                        player.myPlayer.Play(player.movie);
                    }
                    else if (this.MenuState == KinectMenuState.Active && (IconsSprites[4].ButtonState == KinectButtonState.Selected || IconsSprites[5].ButtonState == KinectButtonState.Selected))
                    {
                        if (sending.myPlayer.State != MediaState.Playing)
                        {
                            if ((IconsSprites[4].ButtonState == KinectButtonState.Selected &&
                            IconsSprites[4].LastButtonState != KinectButtonState.Selected) ||
                            (IconsSprites[5].ButtonState == KinectButtonState.Selected &&
                            IconsSprites[5].LastButtonState != KinectButtonState.Selected))
                            {
                                sending.myPlayer.IsLooped = false;
                                sending.myPlayer.Play(sending.movie);

                                sending.AnimationDisplayState = KinectVideoPlayerState.Visible;
                            }
                        }
                        unselectFakeButtons = true;
                    }
                    else
                    {
                        player.myPlayer.Stop();
                        player.VideoPlayerState = KinectVideoPlayerState.Colasped;

                    }
                }
                if (MenuState == KinectMenuState.Iniciated)
                {

                    if (AnimatedRect.Width < Background.Width)
                    {
                        width += Jupiter.AtXSeconds(0.5f, Background.Width);
                        AnimatedRect.Width = (int)width;
                    }
                    else
                        if (AnimatedRect.Height < Background.Height)
                        {
                            height += Jupiter.AtXSeconds(0.5f, Background.Height);
                            AnimatedRect.Height = (int)height;
                        }
                        else
                        {
                            MenuState = KinectMenuState.Active;

                            this.height = 0;
                            this.width = 0;
                            if (IsCloseToTalayot)
                            {
                                //player.MovieState = KinectMovieState.Playing;
                                player.myPlayer.Play(player.movie);
                                this.IconsSprites[1].ButtonState = KinectButtonState.Selected;
                            }
                        }

                    count++;
                }
                else if (MenuState == KinectMenuState.Colapsed)
                {
                    AnimatedRect = new Rectangle(Background.X, Background.Y, 0, 10);
                    foreach (KinectButton kb in IconsSprites)
                    {
                        kb.ButtonState = KinectButtonState.InActive;
                    }
                    this.player.myPlayer.Stop();
                }
                else
                {
                    count = 0;

                    //Esto para comprobar player
                    player.Update();
                    sending.Update();
                    player.CheckButtons(flatHands.HandPosPoint);
                }
                bool[] nowUnSelected = new bool[IconsSprites.Length];
                //funciona eset valor
                bool newSelectedFound = false;
                for (int i = 0; i < IconsSprites.Length; i++)
                {
                    IconsSprites[i].CheckActivity(flatHands.HandPosPoint);
                    IconsSprites[i].Update();
                    //si se ha encontrado uno nuevo ponlos todos a inactive
                    if (newSelectedFound)
                    {
                        IconsSprites[i].ButtonState = KinectButtonState.InActive;
                    }

                    //si esta selecionado per no lo estava todos los demas pasan a ser no selecionados
                    if (IconsSprites[i].ButtonState == KinectButtonState.Selected && wasUnSelected[i])
                    {
                        //he encontrado un nuevo y desselecciono todos los demas
                        newSelectedFound = true;
                        //pongo todos los anteriores a inactive
                        for (int n = 0; n < i; n++)
                        {
                            IconsSprites[n].ButtonState = KinectButtonState.InActive;
                            nowUnSelected[n] = false;
                        }
                    }
                    //si esta selecionado se pone a false unselected

                    if (IconsSprites[i].ButtonState == KinectButtonState.Selected)
                    {
                        nowUnSelected[i] = false;
                    }
                    else
                        nowUnSelected[i] = true;
                }
                //despuede bucle hago otro par garabar historial
                //grabo en mi historial

                for (int i = 0; i < nowUnSelected.Length; i++)
                {
                    wasUnSelected[i] = nowUnSelected[i];
                }

                if (unselectFakeButtons)
                {
                    unselectFakeButtons = false;
                    IconsSprites[5].ButtonState = KinectButtonState.InActive;
                    IconsSprites[4].ButtonState = KinectButtonState.InActive;
                    IconsSprites[4].LastButtonState = KinectButtonState.InActive;
                    IconsSprites[5].LastButtonState = KinectButtonState.InActive;
                }
            }
            
        }
        public void LounchInfoVideo()
        {
            if (player.VideoPlayerState == KinectVideoPlayerState.Colasped)
            {
                player.VideoPlayerState = KinectVideoPlayerState.Visible;
                player.myPlayer.Play(player.movie);
            }


            player.Update();
        }
        public void Draw()
        {
            if (MenuState == KinectMenuState.Active)
            {
                foreach (KinectButton s in IconsSprites)
                {
                    s.Draw();
                }
                player.Draw();
                sending.Draw();
            }
            else
            {
                player.VideoPlayerState = KinectVideoPlayerState.Colasped;
            }
        }

    }
}
